package com.example.frameboot.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author: wukai.bj@centaline.com.cn
 * @date 2020/7/24 13:55
 */
@Service("HttpUtils")
public class HttpUtils {
//    @Autowired
//    private RestTemplate restTemplate;
//
//
////
////    /**
////     * exchange
////     * @return
////     */
////    public String doExchange(String url, Integer age, String name){
////        //header参数
////        HttpHeaders headers = new HttpHeaders();
////        String token = "asdfaf2322";
////        headers.add("authorization", token);
////        headers.setContentType(MediaType.APPLICATION_JSON);
////
////        //放入body中的json参数
////        JSONObject obj = new JSONObject();
////        obj.put("age", age);
////        obj.put("name", name);
////
////        //组装
////        HttpEntity<JSONObject> request = new HttpEntity<>(obj, headers);
////        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
////        String body = responseEntity.getBody();
////        return body;
////    }
//
//    public ResponseEntity<String> doPost(String url, Map<String, String> params) {
//        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
//        for (Map.Entry<String, String> entry : params.entrySet()) {
//            map.add(entry.getKey(), entry.getValue());
//        }
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
//        ResponseEntity<String> str = restTemplate.postForEntity(url, request, String.class);
//        return str;
//    }
//
//    public ResponseEntity<String> handelPost(String url, String body) {
//        RestTemplate restTemplate = new RestTemplate();
//        HttpHeaders headers = new HttpHeaders();
//        HttpEntity<String> formEntity = new HttpEntity<>(body, headers);
//        ResponseEntity<String> str = restTemplate.postForEntity(url, formEntity, String.class);
//        return str;
//    }
}
